﻿using System;
using System.Collections.Generic;

namespace GraphDrawing.Graph.Behaviors
{
    public class GraphObjectViewContainer : IGraphObjectViewContainer
    {
        /// <summary>
        /// Инвариантный ключ.
        /// </summary>
        static readonly object INVARIANT = new object();

        /// <summary>
        /// Словарь объектов контейнера.
        /// </summary>
        readonly Dictionary<Tuple<Type, object>, object> objects = new Dictionary<Tuple<Type, object>, object>();

        public T GetView<T>(object key = null)
        {
            var objKey = key ?? INVARIANT;
            return (T) objects[new Tuple<Type, object>(typeof (T), objKey)];
        }

        public void Add<T>(T view, object key = null)
        {
            var objKey = key ?? INVARIANT;
            objects[new Tuple<Type, object>(typeof (T), objKey)] = view;
        }

        public void Remove<T>(object key = null)
        {
            var objKey = key ?? INVARIANT;
            objects.Remove(new Tuple<Type, object>(typeof (T), objKey));
        }
    }
}